package com.aimaterials.resource.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

public class QRCodeGenerator {
    


    private static final String QR_CODE_IMAGE_PATH = "d:/MyQRCode/";

    public static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.MARGIN, 1);

        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints);

        Path path = FileSystems.getDefault().getPath(filePath);

        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);

    }

    public static byte[] getQRCodeImage(String text, int width, int height) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);

        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
        byte[] pngData = pngOutputStream.toByteArray();
        return pngData;
    }

    public static void main(String[] args) {
        String test="93b681dcaf0411ec9141fa163e78d6b9\n" +
                "93b6dbecaf0411ec9141fa163e78d6b9\n" +
                "93b71cc3af0411ec9141fa163e78d6b9\n" +
                "93b75afbaf0411ec9141fa163e78d6b9\n" +
                "93b79c63af0411ec9141fa163e78d6b9\n" +
                "93b7d9a6af0411ec9141fa163e78d6b9\n" +
                "93b817beaf0411ec9141fa163e78d6b9\n" +
                "93b85794af0411ec9141fa163e78d6b9\n" +
                "93b8941aaf0411ec9141fa163e78d6b9\n" +
                "93b8d21caf0411ec9141fa163e78d6b9\n" +
                "93b91158af0411ec9141fa163e78d6b9\n" +
                "93b97b09af0411ec9141fa163e78d6b9\n" +
                "93b9c005af0411ec9141fa163e78d6b9\n" +
                "93ba210baf0411ec9141fa163e78d6b9\n" +
                "93ba634faf0411ec9141fa163e78d6b9\n" +
                "93baa3d4af0411ec9141fa163e78d6b9\n" +
                "93bade90af0411ec9141fa163e78d6b9\n" +
                "93bb18afaf0411ec9141fa163e78d6b9\n" +
                "93bb5408af0411ec9141fa163e78d6b9\n" +
                "93bb8de5af0411ec9141fa163e78d6b9\n" +
                "93bbf936af0411ec9141fa163e78d6b9";
        String name="城市轨道交通车辆的组成\n" +
                "客室门结构与主要功能\n" +
                "客室车门控制\n" +
                "客室车门故障处理\n" +
                "司机室紧急疏散门的操作\n" +
                "转向架的组成\n" +
                "弹簧悬挂装置\n" +
                "车钩的作用\n" +
                "国产密接式车钩缓冲装置\n" +
                "密接式车钩内部结构与作用原理\n" +
                "全自动车钩缓冲原理\n" +
                "半自动钩缓装置\n" +
                "认知制动系统\n" +
                "闸瓦制动\n" +
                "盘形制动装置的组成\n" +
                "直通式空气制动机工作原理\n" +
                "自动式空气制动机\n" +
                "活塞式空气压缩机工作原理\n" +
                "PC7Y型踏面单元制动器\n" +
                "PC7YF型踏面单元制动器\n" +
                "车辆空调系统主要部件\n";
        String[] title=name.split("\n");
        String[] content=test.split("\n");


        try {
            for(int i=0;i< content.length;i++){
                System.out.println(content[i]);
                generateQRCodeImage(content[i], 425, 425, QR_CODE_IMAGE_PATH+title[i]+".png");
            }
//            generateQRCodeImage("This is my first QR Code", 425, 425, QR_CODE_IMAGE_PATH);
        } catch (WriterException e) {
            System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
        }

    }


}